# -*- coding: utf-8 -*-
import datetime
import time

from PageClass.boe_NewFundCollectionClass.Class_NewFundCollectionBoe import Class_NewFundCollectionBoe
from TestCases.common import LoginDepend
from TestCases.common._baseClass import _BaseClass
from Util import config, logger, record, util

# 收款报账单
class BoeSubmit(_BaseClass):

    def __init__(self, isExit=True):
        self.isExit = isExit
        self.login = LoginDepend('basHost', 'user')
        self.NewFundCollection = Class_NewFundCollectionBoe(self.login.driver)

    def __exit__(self, *args):
        if super().__exit__(*args):
            if self.isExit:
                self.exit()
        else:
            self.exit()

    def exit(self):
        self.NewFundCollection.driver.quit()

    def submit(self, boe_data):
        """添加一笔流水"""
        # 打开流水认领-流水管理
        logger.info(f"流水认领地址为：{config.urlDict.url.claimHost}")  # .\Config\test\UrlConfig.yml
        claimHost = config.urlDict.url.claimHost  # str
        """流水认领-流水管理界面"""
        self.NewFundCollection.goToBoePage(claimHost+'/statementManage')
        # 点击手工添加按钮
        self.NewFundCollection.clickAddByHandle()
        # # '手工添加'弹框-银行流水号
        hostId = util.gen_random_str()
        self.NewFundCollection.input_hostid(hostId)
        logger.info("'手工添加'弹框-收款方账号>>>>>>")
        # '手工添加'弹框-收款方账号，默认选第一个
        self.NewFundCollection.click_receiptAccount()
        self.NewFundCollection.selectValueFromSmallTable()
        # '手工添加'弹框-金额
        self.NewFundCollection.input_itemamount(boe_data.itemamount)
        # '手工添加'弹框-交易时间
        self.NewFundCollection.input_actDate(time.strftime('%Y-%m-%d %H:%M:%S'))  # 判重了，故取实时时间
        # '手工添加'弹框-摘要
        self.NewFundCollection.input_explainInfo(boe_data.explainInfo)
        # '手工添加'弹框-提交按钮
        self.NewFundCollection.click_submit_button()
        """流水发布"""
        """流水认领-流水清分界面"""
        self.NewFundCollection.goToBoePage(claimHost+'/statementDistribution')
        time.sleep(2)
        try:
            # 流水认领-条件查询-展开
            self.NewFundCollection.click_bigger_window()
            # 输入查询条件-流水id
            self.NewFundCollection.input_undefined_hostId(hostId)
            # 输入查询条件-摘要
            self.NewFundCollection.input_undefined_explainInfo(boe_data.explainInfo)
            # 点击查询
            self.NewFundCollection.click_search_button()
        except:
            pass
        # 选第一行
        self.NewFundCollection.click_allselect()
        # 流水发布
        self.NewFundCollection.click_fbbutton()
        # 确认发布
        self.NewFundCollection.click_double_ok_button()
        """待认领菜单"""
        """待认领-收款流水界面"""
        self.NewFundCollection.goToBoePage(claimHost+'/toBeClaimedStatements')
        self.NewFundCollection.wait_table_or_page1()
        try:
            # 待认领菜单-条件查询-展开
            self.NewFundCollection.click_bigger_window_e()
            # 输入查询条件-流水id
            self.NewFundCollection.input_undefined_hostId(hostId)
            # 待认领菜单-查询条件-摘要
            self.NewFundCollection.input_undefined_explainInfo_e(boe_data.explainInfo)
            # 待认领菜单-查询条件-流水状态
            self.NewFundCollection.click_claimStatus()
            # 待认领菜单-查询条件-查询按钮
            self.NewFundCollection.click_search_button_e()
        except:
            pass
        # 点击第一行的来款确认按钮,进入流水详情页面
        self.NewFundCollection.click_double_moneyok_button()
        """流水详情页面"""
        # 等待页面加载
        self.NewFundCollection.wait_table_or_page2()
        # 流水详情页面-本次确认信息-往来方字段，点击  # small弹框
        self.NewFundCollection.click_goandto()
        time.sleep(2)
        self.NewFundCollection.selectValueFromSmallTable(**{'客商编码': boe_data.vendor_code})
        # 流水详情页面-本次确认信息-本次确认金额字段
        self.NewFundCollection.input_thistime_money(boe_data.itemamount)
        # 流水详情页面-本次确认信息-款项类型字段  # small弹框
        self.NewFundCollection.click_moneytype()
        self.NewFundCollection.selectValueFromSmallTable(**{'款项类型名称': boe_data.operationType})
        # 流水详情页面-本次确认信息-备注字段
        self.NewFundCollection.input_this_remark(boe_data.explainInfo)
        # 流水详情页面-本次确认信息-流水确认按钮
        self.NewFundCollection.input_this_ok_button()
        # 勾选待认领的收款拆分行勾选框
        self.NewFundCollection.click_flex_1()
        # 点击流水认领按钮
        self.NewFundCollection.click_this_get_button()
        # 点击”跳过核销按钮“（系统检测有未核销应收账款，请确认是否进入核销界面？）
        try:
            self.NewFundCollection.click_InButton()
        except:
            pass
        """收款报账单填单页面"""
        # 等待页面加载
        try:
            self.NewFundCollection.wait_table_or_page3()
        except:
            pass
        # 收款报账单填单页面-各区域信息填写。登记收入填不了就删掉
        # try:
        #     self.NewFundCollection.click_operationSubType("费用明细", boe_data.itemamount)
        #     self.NewFundCollection.click_operationSubType("登记预收", boe_data.itemamount)
        #     self.NewFundCollection.click_operationSubType("登记收入", boe_data.itemamount)
        #     # 收款报账单填单页面-登记收入-成本中心  # small弹框选第一条
        #     self.NewFundCollection.click_oldmoney_center()
        #     self.NewFundCollection.selectValueFromSmallTable()
        #     # 收款报账单填单页面-下一步
        #     self.NewFundCollection.click_nextstep_button()
        # except:
        #     # 选成本中心失败了，就关闭弹框
        #     self.NewFundCollection.click_close_button()
        #     self.NewFundCollection.click_operationSubType("费用明细", boe_data.itemamount)
        #     self.NewFundCollection.click_operationSubType("登记预收", boe_data.double_itemamount)
        #     # 收款报账单填单页面-登记收入-删除行
        #     self.NewFundCollection.click_del_3()
        #     # 收款报账单填单页面-下一步
        #     self.NewFundCollection.click_nextstep_button()

        self.NewFundCollection.click_operationSubType("费用明细", boe_data.itemamount)
        self.NewFundCollection.click_operationSubType("登记预收", boe_data.itemamount)
        self.NewFundCollection.click_operationSubType("登记收入", boe_data.itemamount)
        try:
            # 收款报账单填单页面-登记收入-成本中心  # small弹框选第一条
            self.NewFundCollection.click_oldmoney_center()
            self.NewFundCollection.selectValueFromSmallTable()
        except:
            # 选成本中心失败
            # 关闭选成本中心弹框
            self.NewFundCollection.click_close_button()
            # 收款报账单填单页面-登记收入-删除行
            self.NewFundCollection.click_del_3()
            # 收款报账单填单页面-登记预收翻倍填值
            self.NewFundCollection.click_operationSubType("登记预收", boe_data.double_itemamount)
        # 收款报账单填单页面-下一步
        self.NewFundCollection.click_nextstep_button()


        # 收款报账单单据预览页面-提交
        message = self.NewFundCollection.click_end_button()
        # # 提交单据
        # message = self.NewFundCollection.click_end_button()
        # return message

        # 记录最新的收款报账单信息
        util.getNewFundCollectionBoeInfo('收款报账单')

        return message
